package com.web.sys.sercice.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.web.sys.dao.ISysGroupDao;
import com.web.sys.model.SysGroup;
import com.web.sys.sercice.ISysGroupService;

@Service("sysGroupService")
public class SysGroupService implements ISysGroupService {
	
	@Inject
	private ISysGroupDao sysGroupDao;
	

	@Override
	public SysGroup createGroup(SysGroup g) {
		return sysGroupDao.add(g);
	}

	@Override
	public SysGroup getGroup(String gid) {
		
		return sysGroupDao.load(gid);
	}

	@Override
	public List<SysGroup> getGroupByUserId(String uid) {
		String sql="select grp_id from sys_usr_grp_rel where usr_id=?";
		String hql="from SysGroup where id in(:ids)";
		List<Object[]> gids=sysGroupDao.queryList(sql, uid);
		List<Object> gls=new ArrayList<Object>();
		for(Object o:gids){
			gls.add(o);
		}
		Map<String,Object> alias=new HashMap<String, Object>();
		alias.put("ids", gls);
		return sysGroupDao.listByAlias(hql, alias);
	}

	@Override
	public void modifyGroup(SysGroup g) {
		sysGroupDao.update(g);
	}

}
